import { redirect, type LoaderFunction } from 'react-router';

export const AuthGuard: LoaderFunction = ({ request }) => {
  const token = true;

  const path = new URL(request.url).pathname;

  // 如果未认证，重定向到登录页并记录来源
  if (token) {
    if (path === '/login') {
      return redirect('/');
    } else {
      // console.log('添加动态路由');
    }
  } else {
    return redirect('/login?redirect=' + path);
  }

  return null;
};
